package com.qihang.util;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.qihang.core.domain.UserEntity;
import com.qihang.web.security.CommonInterceptor;


public class RequestUtil {
	
	private static Logger logger = Logger.getLogger(RequestUtil.class);
	
	private  static  final  Base64  base64 = new Base64(true); 
	
	 public  static  final  String  LAST_PAGE = "lastPage";//未登录时访问的页面 
	 
	 public  static  final  String  REDIRECT_HOME = "/";//未登录时跳转到首页 
	 
	 public  static  final  String  LOGIN_HOME = "index";//登录成功后进入的页面 
	 
	private  HttpServletRequest request;
	private  HttpServletResponse response;

	private final String SESSION_USER = "user";

	/**
	 * 获取request
	 * 
	 * @return
	 */
	public HttpServletRequest getRequest() {
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = ((ServletRequestAttributes) ra)
				.getRequest();
		return request;
	}
	

	/**
	 * 获取session
	 * 
	 * @retur
	 * n
	 */
	public HttpSession getSession() {
		HttpSession session = this.getRequest().getSession();
		return session;
	}
	
	/**
	 * 获取用户信息
	 * <p>
	 * 
	 * 
	 * 此方法不捕获是否存在用户信息,若不存在则报错
	 * </p>
	 * @return
	 */
	public UserEntity getUserInfo() {
		UserEntity user = (UserEntity) this.getSession().getAttribute(SESSION_USER);
		return user;
	}
	
	 /** 
     * 保存当前请求 
     */ 
	public static void saveRequest(HttpServletRequest request) {
	   request.getSession().setAttribute(LAST_PAGE, RequestUtil.hashRequestPage(request)); 
	   logger.debug("被拦截的url的sessionID:"+ request.getSession().getId()); 
	   logger.debug("save request for "+ request.getRequestURI()); 
		
	}
	
	/** 
     * 加密请求页面 
     * @param request 
     * @return 
     */ 
    public static  String hashRequestPage(HttpServletRequest request) { 
        String reqUri = request.getRequestURI(); 
        String query = request.getQueryString(); 
        if (query != null) { 
            reqUri += "?" + query; 
        } 
        String targetPage = null; 
        try { 
            targetPage = base64.encodeAsString(reqUri.getBytes("UTF-8")); 
        } catch (UnsupportedEncodingException ex) { 
            //this does not happen 
        } 
        return targetPage; 
    } 
    
    /** 
     * 取出之前保存的请求 
     * @return 
     */ 
    public static String retrieveSavedRequest(HttpServletRequest request) { 
        HttpSession session = request.getSession(); 
        if (session == null) { 
            return REDIRECT_HOME; 
        } 
        String HashedlastPage = (String) session.getAttribute(LAST_PAGE); 
        if (HashedlastPage == null) { 
            return LOGIN_HOME; 
        } else { 
            return retrieve(HashedlastPage); 
        } 
    } 
    
    /** 
     * 解密请求的页面 
     * @param targetPage 
     * @return 
     */ 
    public static String retrieve(String targetPage) { 
        byte[] decode = base64.decode(targetPage); 
        try { 
            String requestUri = new String(decode, "UTF-8"); 
            int i = requestUri.indexOf("/", 1); 
            return requestUri.substring(i); 
        } catch (UnsupportedEncodingException ex) { 
            //this does not happen 
            return null; 
        } 
    } 
}
